package com.dianrui.qiyouriding.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.dianrui.qiyouriding.Constant;
import com.dianrui.qiyouriding.R;
import com.dianrui.qiyouriding.okhttp.OkHttpRequest;
import com.dianrui.qiyouriding.util.StringUtils;
import com.dianrui.qiyouriding.util.ToastUtil;
import com.dianrui.qiyouriding.util.Url;
import com.google.gson.JsonObject;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 编码借车
 */

public class CodeCarActivity extends BaseActivity {
    private  Double lat;
    private Double lng;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.code_car_edit)
    EditText codeCarEdit;

    @Override
    public int getContentViewId() {
        return R.layout.activity_code_car;
    }

    @Override
    public void initView() {
        if (getIntent() != null && getIntent().getExtras() != null) {
            Bundle bundle=getIntent().getExtras();
            if (bundle != null) {
                lat = bundle.getDouble("lat");
                lng = bundle.getDouble("lng");
            }
        }
        customInit(true, R.color.black_383838);
        codeCarEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2){ }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 10) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null) {
                        imm.hideSoftInputFromWindow(codeCarEdit.getWindowToken(), 0);
                    }
                }
            }
        });
    }

    @Override
    public void initData() {}


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


    @OnClick({R.id.back, R.id.sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.sure:
                if (StringUtils.isEmpty(codeCarEdit.getText().toString())) {
                    ToastUtil.showToast(getString(R.string.code_car_hint1));
                } else {
                    startCode(codeCarEdit.getText().toString());
                }
                break;

                default:
                    break;
        }
    }

    private void startCode(final String devicenumbers) {
        JsonObject detectionJson = new JsonObject();
        detectionJson.addProperty("number", codeCarEdit.getText().toString());
        detectionJson.addProperty("is_remote", 1);
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("member_id"))) {
            detectionJson.addProperty("member_id", SPUtils.getInstance().getString("member_id"));
        }
        OkHttpRequest.getInstance().postRequests(Url.CHECK_BIKE, detectionJson.toString(),new OkHttpRequest.XCallBack() {
            @Override
            public void onResponse(String response) {
                if (!StringUtils.isEmpty(response)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(response);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                Intent intent = new Intent(CodeCarActivity.this, ScanInfoActivity.class);
                                Bundle bundle=new Bundle();
                                bundle.putString("devicenumber", devicenumbers);
                                bundle.putString("isRomote", "2");
                                bundle.putString("vehicle_status", "2");
                                bundle.putDouble("lat",lat);
                                bundle.putDouble("lng",lng);
                                intent.putExtras(bundle);
                                startActivity(intent);
                                finish();
                            } else {
                                ToastUtil.showToast(jsonObject.optString("message"));
                            }

                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }

}
